﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Visifire.Charts;
using TestMatrix.Model;
using TestMatrix.BLController;

namespace TestMatrix.Phone
{
    public partial class TestCaseReportPivot : PhoneApplicationPage
    {
        public TestCaseReportPivot()
        {
            InitializeComponent();
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            string prjName = "";
            base.OnNavigatedTo(e);
            if (!e.NavigationMode.Equals(NavigationMode.Back) & NavigationContext.QueryString.TryGetValue("PrjName", out prjName))
            {
                ShowMonthlyTestCaseTrendLine(RenderAs.Line, prjName);
                ShowCreatedTCSplineChart(RenderAs.Line, prjName);
                ShowMost7TestCaseExecLine(RenderAs.Line, prjName);

                //ShowMonthlyTestCaseTrend(RenderAs.StackedBar, prjName);
                //ShowMost7TestCaseExecBar(RenderAs.StackedBar, prjName);
            }
        }

        private void ShowCreatedTCSplineChart(RenderAs ra, string prjName)
        {
            string sql = "select * from tblNumOfCreatedTC where Application=? order by CreatedDate desc limit 7;";

            List<tblNumOfCreatedTC> listDatas = ChartController.QueryTblNumOfCreatedTC(sql, prjName);

            DataSeries dataSeries = new DataSeries();
            dataSeries.RenderAs = ra;
            dataSeries.ShadowEnabled = true;
            dataSeries.LabelEnabled = true;

            DataPoint dp;
            for (int i = 0; i < listDatas.Count(); i++)
            {
                dp = new DataPoint();
                dp.AxisXLabel = listDatas[i].CreatedDate;
                //dp.AxisXLabel = listDatas[i].BugType;
                dp.YValue = listDatas[i].NumCreatedTC;
                dataSeries.DataPoints.Add(dp);
            }

            //BugTypeChart.View3D = true;
            TestCaseCreatedSplineChart.Series.Add(dataSeries);
        }

        

        private void ShowMonthlyTestCaseTrendLine(RenderAs ra, string prjName)
        {
            // TotalTestCases
            DataSeries dataSeries1 = new DataSeries();

            dataSeries1.RenderAs = ra;
            dataSeries1.MarkerType = Visifire.Commons.MarkerTypes.Circle;
            dataSeries1.SelectionEnabled = true;
            dataSeries1.LineThickness = 3;
            dataSeries1.ShowInLegend = true;
            dataSeries1.LabelEnabled = true;
            dataSeries1.IncludePercentageInLegend = true;

            dataSeries1.LabelText = "#YValue";
            dataSeries1.LegendText = "TotalTestCases";
            string strDataPoint1 = "select CreatedDate as CreatedDate,TotalTestCases from tblTestAutomation where Application=?";

            List<tblTestAutomation> listDataPoints1 = ChartController.QueryTblTestAutomation(strDataPoint1, prjName);

            DataPoint dp;
            for (int i = 0; i < listDataPoints1.Count(); i++)
            {
                dp = new DataPoint();
                dp.AxisXLabel = listDataPoints1[i].CreatedDate;
                dp.YValue = listDataPoints1[i].TotalTestCases;
                dp.Exploded = true;
                dataSeries1.DataPoints.Add(dp);
            }
            LineMonthlyTestCaseTrend.Series.Add(dataSeries1);

            // Automatable
            DataSeries dataSeries2 = new DataSeries();

            dataSeries2.RenderAs = ra;
            dataSeries2.MarkerType = Visifire.Commons.MarkerTypes.Circle;
            dataSeries2.SelectionEnabled = true;
            dataSeries2.LineThickness = 3;
            dataSeries2.ShowInLegend = true;
            dataSeries2.LabelEnabled = true;
            dataSeries2.IncludePercentageInLegend = true;

            dataSeries2.LabelText = "#YValue";
            dataSeries2.LegendText = "Automatable";
            string strDataPoint2 = "select CreatedDate as CreatedDate,Automatable from tblTestAutomation where Application=?";

            List<tblTestAutomation> listDataPoints2 = ChartController.QueryTblTestAutomation(strDataPoint2, prjName);

            for (int i = 0; i < listDataPoints2.Count(); i++)
            {
                dp = new DataPoint();
                dp.AxisXLabel = listDataPoints2[i].CreatedDate;
                dp.YValue = listDataPoints2[i].Automatable;
                dp.Exploded = true;
                dataSeries2.DataPoints.Add(dp);
            }
            LineMonthlyTestCaseTrend.Series.Add(dataSeries2);

            // AlreadyAutomated
            DataSeries dataSeries3 = new DataSeries();

            dataSeries3.RenderAs = ra;
            dataSeries3.MarkerType = Visifire.Commons.MarkerTypes.Circle;
            dataSeries3.SelectionEnabled = true;
            dataSeries3.LineThickness = 3;
            dataSeries3.ShowInLegend = true;
            dataSeries3.LabelEnabled = true;
            dataSeries3.IncludePercentageInLegend = true;

            dataSeries3.LabelText = "#YValue";
            dataSeries3.LegendText = "AlreadyAutomated";
            string strDataPoint3 = "select CreatedDate as CreatedDate,AlreadyAutomated from tblTestAutomation where Application=?";

            List<tblTestAutomation> listDataPoints3 = ChartController.QueryTblTestAutomation(strDataPoint3, prjName);

            for (int i = 0; i < listDataPoints3.Count(); i++)
            {
                dp = new DataPoint();
                dp.AxisXLabel = listDataPoints3[i].CreatedDate;
                dp.YValue = listDataPoints3[i].AlreadyAutomated;
                dp.Exploded = true;
                dataSeries3.DataPoints.Add(dp);
            }
            LineMonthlyTestCaseTrend.Series.Add(dataSeries3);
        }

        private void ShowMost7TestCaseExecLine(RenderAs ra, string prjName)
        {
            // Total Executed
            DataSeries dataSeries1 = new DataSeries();

            dataSeries1.RenderAs = ra;
            dataSeries1.MarkerType = Visifire.Commons.MarkerTypes.Circle;
            dataSeries1.SelectionEnabled = true;
            dataSeries1.LineThickness = 3;
            dataSeries1.ShowInLegend = true;
            dataSeries1.LabelEnabled = true;
            dataSeries1.IncludePercentageInLegend = true;

            dataSeries1.LabelText = "#YValue";
            dataSeries1.LegendText = "Total Executed";
            string strDataPoint1 = "select * from tblNumOfExecutedTC where Application=? order by ExecutedStartDt desc limit 7;";

            List<tblNumOfExecutedTC> listDataPoints1 = ChartController.QuerytblNumOfExecutedTC(strDataPoint1, prjName);

            DataPoint dp;
            for (int i = 0; i < listDataPoints1.Count(); i++)
            {
                dp = new DataPoint();
                dp.AxisXLabel = listDataPoints1[i].ExecutedStartDt;
                dp.YValue = listDataPoints1[i].NumExecutedTC;
                dp.Exploded = true;
                dataSeries1.DataPoints.Add(dp);
            }
            LineTestCaseExec.Series.Add(dataSeries1);

            // total passed
            DataSeries dataSeries2 = new DataSeries();

            dataSeries2.RenderAs = ra;
            dataSeries2.MarkerType = Visifire.Commons.MarkerTypes.Circle;
            dataSeries2.SelectionEnabled = true;
            dataSeries2.LineThickness = 3;
            dataSeries2.ShowInLegend = true;
            dataSeries2.LabelEnabled = true;
            dataSeries2.IncludePercentageInLegend = true;

            dataSeries2.LabelText = "#YValue";
            dataSeries2.LegendText = "Num of Passed";
            string strDataPoint2 = "select * from tblNumOfExecutedTC where Application=? order by ExecutedStartDt desc limit 7;";

            List<tblNumOfExecutedTC> listDataPoints2 = ChartController.QuerytblNumOfExecutedTC(strDataPoint2, prjName);

            for (int i = 0; i < listDataPoints2.Count(); i++)
            {
                dp = new DataPoint();
                dp.AxisXLabel = listDataPoints2[i].ExecutedStartDt;
                dp.YValue = listDataPoints2[i].NumPassed;
                dp.Exploded = true;
                dataSeries2.DataPoints.Add(dp);
            }
            LineTestCaseExec.Series.Add(dataSeries2);

            // total failed
            DataSeries dataSeries3 = new DataSeries();

            dataSeries3.RenderAs = ra;
            dataSeries3.MarkerType = Visifire.Commons.MarkerTypes.Circle;
            dataSeries3.SelectionEnabled = true;
            dataSeries3.LineThickness = 3;
            dataSeries3.ShowInLegend = true;
            dataSeries3.LabelEnabled = true;
            dataSeries3.IncludePercentageInLegend = true;

            dataSeries3.LabelText = "#YValue";
            dataSeries3.LegendText = "Num of Failed";
            string strDataPoint3 = "select * from tblNumOfExecutedTC where Application=? order by ExecutedStartDt desc limit 7;";

            List<tblNumOfExecutedTC> listDataPoints3 = ChartController.QuerytblNumOfExecutedTC(strDataPoint3, prjName);

            for (int i = 0; i < listDataPoints3.Count(); i++)
            {
                dp = new DataPoint();
                dp.AxisXLabel = listDataPoints3[i].ExecutedStartDt;
                dp.YValue = listDataPoints3[i].NumFailed;
                dp.Exploded = true;
                dataSeries3.DataPoints.Add(dp);
            }
            LineTestCaseExec.Series.Add(dataSeries3);
        }


        #region retired
        /*
        private void ShowMost7TestCaseExecBar(RenderAs ra, string prjName)
        {
            // Total Executed
            DataSeries dataSeries1 = new DataSeries();

            dataSeries1.RenderAs = ra;
            dataSeries1.ShowInLegend = true;
            dataSeries1.LabelEnabled = true;
            dataSeries1.IncludePercentageInLegend = true;

            dataSeries1.LabelText = "#YValue";
            dataSeries1.LegendText = "Total Executed";
            string strDataPoint1 = "select * from tblNumOfExecutedTC where Application=? order by ExecutedStartDt desc limit 7;";

            List<tblNumOfExecutedTC> listDataPoints1 = ChartController.QuerytblNumOfExecutedTC(strDataPoint1, prjName);

            DataPoint dp;
            for (int i = 0; i < listDataPoints1.Count(); i++)
            {
                dp = new DataPoint();
                dp.AxisXLabel = listDataPoints1[i].ExecutedStartDt;
                dp.YValue = listDataPoints1[i].NumExecutedTC;
                dp.Exploded = true;
                dataSeries1.DataPoints.Add(dp);
            }
            Most7TestCaseExecTrend.Series.Add(dataSeries1);

            // total passed
            DataSeries dataSeries2 = new DataSeries();

            dataSeries2.RenderAs = ra;
            dataSeries2.ShowInLegend = true;
            dataSeries2.LabelEnabled = true;
            dataSeries2.IncludePercentageInLegend = true;

            dataSeries2.LabelText = "#YValue";
            dataSeries2.LegendText = "Num of Passed";
            string strDataPoint2 = "select * from tblNumOfExecutedTC where Application=? order by ExecutedStartDt desc limit 7;";

            List<tblNumOfExecutedTC> listDataPoints2 = ChartController.QuerytblNumOfExecutedTC(strDataPoint2, prjName);

            for (int i = 0; i < listDataPoints2.Count(); i++)
            {
                dp = new DataPoint();
                dp.AxisXLabel = listDataPoints2[i].ExecutedStartDt;
                dp.YValue = listDataPoints2[i].NumPassed;
                dp.Exploded = true;
                dataSeries2.DataPoints.Add(dp);
            }
            Most7TestCaseExecTrend.Series.Add(dataSeries2);

            // total failed
            DataSeries dataSeries3 = new DataSeries();

            dataSeries3.RenderAs = ra;
            dataSeries3.ShowInLegend = true;
            dataSeries3.LabelEnabled = true;
            dataSeries3.IncludePercentageInLegend = true;

            dataSeries3.LabelText = "#YValue";
            dataSeries3.LegendText = "Num of Failed";
            string strDataPoint3 = "select * from tblNumOfExecutedTC where Application=? order by ExecutedStartDt desc limit 7;";

            List<tblNumOfExecutedTC> listDataPoints3 = ChartController.QuerytblNumOfExecutedTC(strDataPoint3, prjName);

            for (int i = 0; i < listDataPoints3.Count(); i++)
            {
                dp = new DataPoint();
                dp.AxisXLabel = listDataPoints3[i].ExecutedStartDt;
                dp.YValue = listDataPoints3[i].NumFailed;
                dp.Exploded = true;
                dataSeries3.DataPoints.Add(dp);
            }
            Most7TestCaseExecTrend.Series.Add(dataSeries3);
        }
         
        private void ShowMonthlyTestCaseTrend(RenderAs ra, string prjName)
        {
            // TotalTestCases
            DataSeries dataSeries1 = new DataSeries();
            dataSeries1.RenderAs = ra;
            dataSeries1.ShowInLegend = true;
            dataSeries1.LabelEnabled = true;
            dataSeries1.IncludePercentageInLegend = true;

            dataSeries1.LabelText = "#YValue";
            dataSeries1.LegendText = "TotalTestCases";
            string strDataPoint1 = "select CreatedDate as CreatedDate,TotalTestCases from tblTestAutomation where Application=?";

            List<tblTestAutomation> listDataPoints1 = ChartController.QueryTblTestAutomation(strDataPoint1, prjName);

            DataPoint dp;
            for (int i = 0; i < listDataPoints1.Count(); i++)
            {
                dp = new DataPoint();
                dp.AxisXLabel = listDataPoints1[i].CreatedDate;
                dp.YValue = listDataPoints1[i].TotalTestCases;
                dp.Exploded = true;
                dataSeries1.DataPoints.Add(dp);
            }
            MonthlyTestCaseTrend.Series.Add(dataSeries1);


            // Automatable
            DataSeries dataSeries2 = new DataSeries();
            dataSeries2.RenderAs = ra;
            dataSeries2.ShowInLegend = true;
            dataSeries2.LabelEnabled = true;
            dataSeries2.IncludePercentageInLegend = true;

            dataSeries2.LabelText = "#YValue";
            dataSeries2.LegendText = "Automatable";
            string strDataPoint2 = "select CreatedDate as CreatedDate,Automatable from tblTestAutomation where Application=?";

            List<tblTestAutomation> listDataPoints2 = ChartController.QueryTblTestAutomation(strDataPoint2, prjName);
            for (int i = 0; i < listDataPoints2.Count(); i++)
            {
                dp = new DataPoint();
                dp.AxisXLabel = listDataPoints2[i].CreatedDate;
                dp.YValue = listDataPoints2[i].Automatable;
                dp.Exploded = true;
                dataSeries2.DataPoints.Add(dp);
            }
            MonthlyTestCaseTrend.Series.Add(dataSeries2);

            // AlreadyAutomated
            DataSeries dataSeries3 = new DataSeries();

            dataSeries3.RenderAs = ra;
            dataSeries3.ShowInLegend = true;
            dataSeries3.LabelEnabled = true;
            dataSeries3.IncludePercentageInLegend = true;

            dataSeries3.LabelText = "#YValue";
            dataSeries3.LegendText = "AlreadyAutomated";
            string strDataPoint3 = "select CreatedDate as CreatedDate,AlreadyAutomated from tblTestAutomation where Application=?";

            List<tblTestAutomation> listDataPoints3 = ChartController.QueryTblTestAutomation(strDataPoint3, prjName);

            for (int i = 0; i < listDataPoints3.Count(); i++)
            {
                dp = new DataPoint();
                dp.AxisXLabel = listDataPoints3[i].CreatedDate;
                dp.YValue = listDataPoints3[i].AlreadyAutomated;
                dp.Exploded = true;
                dataSeries3.DataPoints.Add(dp);
            }
            MonthlyTestCaseTrend.Series.Add(dataSeries3);
        }
         
         */
        #endregion
    }
}